package vip.xiaonuo.modular.common;

import cn.hutool.core.collection.CollectionUtil;
import org.springframework.util.StringUtils;

import java.awt.*;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: liu
 * @Date:2022/10/26
 */
public class GridColorUtils {
    static String BACKGROUND_COLOR = "#F4F4F4";
    static String FRONT_COLOR = "#00FF00";
    /**
     * 根据一个颜色获取6个梯度颜色
     * @param color2
     * @return
     */
    public static List<String> getColorLevel(String color2) {
        if(StringUtils.isEmpty(color2)){
            color2 = FRONT_COLOR;
        }
        if(color2.length()>7){
            color2 = color2.substring(0,7);
        }
        List<String> list = new ArrayList<>();
        String color = BACKGROUND_COLOR;
        Color c1 = new Color(Integer.parseInt(color.substring(1), 16));
        Color c2 = new Color(Integer.parseInt(color2.substring(1), 16));
        int len = 6;
        for (int i = 0; i < len; i++) {
            int _r = Math.abs((c1.getRed()-c2.getRed())*i/len);
            int r = c1.getRed() < c2.getRed() ? c1.getRed() + _r : c2.getRed() + _r;

            int _g = Math.abs((c1.getGreen()-c2.getGreen())*i/len);
            int g = c1.getGreen() < c2.getGreen() ? c1.getGreen() + _g : c2.getGreen() + _g;

            int _b = Math.abs((c1.getBlue()-c2.getBlue())*i/len);
            int b = c1.getBlue() < c2.getBlue() ? c1.getBlue() + _b : c2.getBlue() + _b;

            Color cc = new Color(r, g, b);
            list.add("#"+Integer.toHexString(cc.getRGB()).substring(2));
        }
        return CollectionUtil.reverse(list);
    }

    /**
     * 获取value值对应的颜色
     * @param colors
     * @param max
     * @param min
     * @param value
     * @return
     */
    public static String getColorByValue(List<String> colors, double max, double min,String value) {
        double v = Double.parseDouble(value);
        if(v == 0){
            return BACKGROUND_COLOR;
        }
        double d = (max - min) / colors.size();
        int index = 0;
        for (int i = colors.size()-1; i >= 0; i--) {
            if (v >= min + i * d) {
                index = i;
                break;
            }
        }
        return colors.get(index);
    }

    public static List<Double> getValueRange(List<String> colors,double max, double min) {
        List<Double> list = new ArrayList<>();
        double d = (max - min) / colors.size();
        for (int i = 0; i <colors.size(); i++) {
            double v = new BigDecimal(min + i * d).setScale(3, 4).doubleValue();
            list.add(v);
        }
        return list;
    }
}
